package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostAreaDao;
import ysq.xyg.host.model.po.HostArea;
import ysq.xyg.host.model.po.HostCity;
import ysq.xyg.host.model.vo.HostCityVo;
import ysq.xyg.host.service.HostCityService;
import ysq.xyg.sys.model.po.SysDepart;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostAreaVo;
import ysq.xyg.host.service.HostAreaService;
import ysq.xyg.sys.model.vo.SysDepartVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 房产模块-区域service实现
 *
 * @author shengQiang yu
 * @date 2019-7-1 13:58:43
 */
@Service("hostAreaService")
public class HostAreaServiceImpl extends BaseServiceImpl<HostArea> implements HostAreaService {
    @Resource
    private HostCityService hostCityService;
    @Resource
    private HostAreaDao hostAreaDao;

    private HostArea hostArea;

    @Autowired
    public void setBaseDao(HostAreaDao hostAreaDao) {
        this.baseDao = hostAreaDao;
    }


    /**
     * 分页查询
     *
     * @param hostAreaVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 13:58:43
     */
    public ReturnEntity page(HostAreaVo hostAreaVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<HostArea> list = hostAreaDao.listLeft(hostAreaVo);
        PageInfo<HostArea> page=new PageInfo<HostArea>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostAreaVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 13:58:43
     */
    public ReturnEntity addOrUpdate(HostAreaVo hostAreaVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostAreaVo.getId())){
            hostArea=new HostArea(currentUser);
            isUpate=false;
        }else{
            hostArea=selectByPrimaryKey(hostAreaVo.getId());
            if(ObjectUtil.isNull(hostArea)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostArea.setUpdateTime(new Date());
            hostArea.setUpdateUser(currentUser.getId());
        }
        hostArea.setName(hostAreaVo.getName());
        hostArea.setCityId(hostAreaVo.getCityId());
        hostArea.setUserId(hostAreaVo.getUserId());
        hostArea.setSort(hostAreaVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostArea);
        }else{
            num=insert(hostArea);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 13:58:43
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostArea hostArea=selectByPrimaryKey(id);
        if(hostArea!=null){
            hostArea.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostArea.setUpdateTime(new Date());
            hostArea.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostArea);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostAreaVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-1 13:58:43
     */
    public List<HostArea> acceptList(HostAreaVo hostAreaVo){
        Example example = new Example(HostArea.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostAreaVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostAreaVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(hostAreaVo.getCityId())){
            criteria.andEqualTo("cityId",hostAreaVo.getCityId());
        }
        if(StringUtil.isNotEmpty(hostAreaVo.getUserId())){
            criteria.andEqualTo("userId",hostAreaVo.getUserId());
        }
        example.setOrderByClause(" update_time desc");
        return hostAreaDao.selectByExample(example);
    }

    /**
     * 获取城市区域树
     *
     * @param hostAreaVo Vo对象
     * @author shengQiang yu
     * @date 2019-4-2 15:43:10
     */
    public ReturnEntity dTreeList(HostAreaVo hostAreaVo) {
        List<HostArea> list = acceptList(hostAreaVo);
        List<Map<String,Object>> dtreeEntityList=new ArrayList<Map<String,Object>>();
        if(StrUtil.isEmpty(hostAreaVo.getAreaIds())){
            List<HostCity> cityList=hostCityService.acceptList(new HostCityVo());
            for(HostCity hostCity:cityList){
                Map<String,Object> dtreeEntity=new HashedMap();
                dtreeEntity.put("id",hostCity.getId());
                dtreeEntity.put("title",hostCity.getName());
                dtreeEntity.put("parentId","0");
                dtreeEntity.put("isLast",false);
                Map<String,String> m=new HashedMap();
                m.put("type","0");
                m.put("isChecked","0");
                List<Map<String,String>> dtreeCheckEntityList=new ArrayList<Map<String,String>>();
                dtreeCheckEntityList.add(m);
                dtreeEntity.put("checkArr",dtreeCheckEntityList);
                dtreeEntityList.add(dtreeEntity);
            }
        }
        for(HostArea hostArea:list){
            if(StrUtil.isEmpty(hostAreaVo.getAreaIds())||hostAreaVo.getAreaIds().contains(hostArea.getId())){
                Map<String,Object> dtreeEntity=new HashedMap();
                dtreeEntity.put("id",hostArea.getId());
                dtreeEntity.put("title",hostArea.getName());
                if(StrUtil.isEmpty(hostAreaVo.getAreaIds())){
                    dtreeEntity.put("parentId", hostArea.getCityId());
                }else{
                    dtreeEntity.put("parentId", "0");
                }
                dtreeEntity.put("isLast",true);
                Map<String,String> m=new HashedMap();
                m.put("type","0");
                m.put("isChecked","0");
                List<Map<String,String>> dtreeCheckEntityList=new ArrayList<Map<String,String>>();
                dtreeCheckEntityList.add(m);
                dtreeEntity.put("checkArr",dtreeCheckEntityList);
                dtreeEntityList.add(dtreeEntity);
            }

        }
        return new ReturnEntity(dtreeEntityList);
    }

}
